package com.example.administrator.szb.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.administrator.szb.R;
import com.example.administrator.szb.adapter.BaseAdapter;
import com.example.administrator.szb.activity.base.BaseActivity;
import com.example.administrator.szb.bean.IndexBean;
import com.example.administrator.szb.bean.YWBDNEWBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.DialogUtil;
import com.example.administrator.szb.util.GetJsonDataUtil;
import com.example.administrator.szb.util.SharedPreferencesUtil;
import com.example.administrator.szb.util.Toasts;
import com.example.administrator.szb.xzyw.BGZRActivity;
import com.example.administrator.szb.xzyw.DXZFActivity;
import com.example.administrator.szb.xzyw.DZJCActivity;
import com.example.administrator.szb.xzyw.FHBSActivity;
import com.example.administrator.szb.xzyw.GGPZActivity;
import com.example.administrator.szb.xzyw.GPZYActivity;
import com.example.administrator.szb.xzyw.GQDRActivity;
import com.example.administrator.szb.xzyw.QYLCActivity;
import com.example.administrator.szb.xzyw.SBZRActivity;
import com.example.administrator.szb.xzyw.XYDKActivity;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by D N on 2017/10/11.
 * xzyw
 */

public class XZYWActivity extends BaseActivity {
    @Bind(R.id.xzyw_image_fh)
    ImageView xzywImageFh;
    @Bind(R.id.xzyw_recyclerview_yw)
    RecyclerView xzywRecyclerviewYw;
    @Bind(R.id.xzyw_button_xyb)
    Button xzywButtonXyb;
    BaseAdapter<IndexBean.DataBean.BusinessBean> adapter;

    int id;//点击下一步需要的id;
    String title;
    int type ;
    int genre;

    public static YWBDNEWBean ywbdBean;
    int counselor_id = 0 ;
    HashMap<Integer,Class> map = new HashMap<>();//保存跳转的layout.
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zxyw_activity);
        ButterKnife.bind(this);
        SampleApplicationLike.getActivitiesInstance().add(this);
        initView();
    }

    private void initView() {
        xzywRecyclerviewYw.setLayoutManager(new GridLayoutManager(this, 3));
        initAdapter();
        xzywRecyclerviewYw.setAdapter(adapter);
    }

    private void initAdapter() {
        final IndexBean indexBean  = (IndexBean) SharedPreferencesUtil.getData(this,"index");
        adapter = new BaseAdapter<IndexBean.DataBean.BusinessBean>(this,indexBean.getData().getBusiness() ) {
            @Override
            public void convert(int position, BaseViewHolder holder, final IndexBean.DataBean.BusinessBean businessBean) {
                ((CheckBox) holder.getView(R.id.xzyw_radiobutton_gpzy)).setText(businessBean.getTitle());
                if (businessBean.getIsChoose() == 0) {
                    ((CheckBox) holder.getView(R.id.xzyw_radiobutton_gpzy)).setChecked(false);
                }
                ((CheckBox) holder.getView(R.id.xzyw_radiobutton_gpzy)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        if (b) {
                            map.clear();
                            for (IndexBean.DataBean.BusinessBean bean : indexBean.getData().getBusiness()) {
                                if (compoundButton.getText().toString().equals(bean.getTitle())) {
                                    bean.setIsChoose(1);
                                    id = businessBean.getId();
                                    type = businessBean.getType();
                                    genre = businessBean.getGenre();
                                    if (bean.getTitle().equals("股票质押")){
                                        map.put(bean.getId(), GPZYActivity.class);
                                    }else if(bean.getTitle().equals("大宗减持")){
                                        map.put(bean.getId(), DZJCActivity.class);
                                    }
                                    else if(bean.getTitle().equals("企业理财")){
                                        map.put(bean.getId(), QYLCActivity.class);
                                    }
                                    else if(bean.getTitle().equals("信用贷款")){
                                        map.put(bean.getId(), XYDKActivity.class);
                                    }
                                    else if(bean.getTitle().equals("过桥短融")){
                                        map.put(bean.getId(), GQDRActivity.class);
                                    }
                                    else if(bean.getTitle().equals("三板转让")){
                                        map.put(bean.getId(), SBZRActivity.class);
                                    }
                                    else if(bean.getTitle().equals("并购转让")){
                                        map.put(bean.getId(), BGZRActivity.class);
                                    }
                                    else if(bean.getTitle().equals("定向增发")){
                                        map.put(bean.getId(), DXZFActivity.class);
                                    }
                                    else if(bean.getTitle().equals("杠杆配资")){
                                        map.put(bean.getId(), GGPZActivity.class);
                                    }else{
                                        map.put(bean.getId(), FHBSActivity.class);
                                        title = bean.getTitle();
                                    }
                                }else{
                                    bean.setIsChoose(0);
                                }
                            }
                            adapter.notifyDataSetChanged();

                        }
                    }
                });
            }

            @Override
            public int getLayoutId(int viewType) {
                return R.layout.view_xzyw_item;
            }
        };
    }


    @OnClick({R.id.xzyw_image_fh, R.id.xzyw_button_xyb})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.xzyw_image_fh:
                finish();
                break;
            case R.id.xzyw_button_xyb:
                try{
                    Class clazz = map.get(id);
                    if (clazz != null){

                        requestData();
                        /**
                         * todo 先加载数据再进行跳转。
                         */
                    }
                }catch (Exception e){

                }

                break;
        }
    }

    private void requestData() {
        DialogUtil.showIsoProgressbar(this,"加载数据中...");
        HashMap<String, String> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("business_id", id + "");
        map.put("counselor_id", counselor_id + "");
        String json = GetJsonDataUtil.simpleMapToJsonStr(map);
        String url = URLAddress.URLBASE + URLAddress.URL_BDYW;
        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, YWBDNEWBean.class, url, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                ywbdBean = (YWBDNEWBean) response;
                DialogUtil.dialog_ios.dismiss();
                //mdata.addAll(ywbdBean.getData().getList());
                if (ywbdBean.getResult() == 1) {

                    Intent intent = new Intent(XZYWActivity.this,SQXMActivity.class);
                    intent.putExtra("title",title);
                    intent.putExtra("id",id);
                    intent.putExtra("type",type);
                    intent.putExtra("genre",genre);
                    startActivity(intent);
                    //initView();
                   // initRecyclerview();
                } else {
                    Toasts.show(XZYWActivity.this, "" + ywbdBean.getErr_msg(), Toast.LENGTH_SHORT);
                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                DialogUtil.dialog_ios.dismiss();
                Toasts.show(XZYWActivity.this, "网络异常", Toast.LENGTH_SHORT);
            }
        });
    }
}
